package com.yantong.pay.yfz.util;

import org.apache.commons.codec.binary.Base64;

import java.io.InputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.util.Enumeration;

/**
 * Created by dhcao on 2018/1/16.
 * 读取证书文件为公私钥
 */
public class KeyUtil {

//    private static KeyStore ks;
//
//
//    static {
//        try{
//             ks = KeyStore.getInstance("PKCS12");
//            // 获得密钥库文件流
//            InputStream is = Config.getPrivateKeyFile();
//            // 加载密钥库
//            ks.load(is, Config.getPrivateKeyPassword().toCharArray());
//
//            // 关闭密钥库文件流
//            is.close();
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//
//    }

    private static KeyStore getKs(String privateKeyFileUri,String privateKeyFilePwd){
        KeyStore ks = null;
        try {
            ks = KeyStore.getInstance("PKCS12");
            // 获得密钥库文件流
            InputStream is = Config.getPrivateKeyFile(privateKeyFileUri);
            // 加载密钥库
            ks.load(is, privateKeyFilePwd.toCharArray());
        }catch (Exception e){
            e.printStackTrace();
        }
        return ks;
    }

    /**
     * 获取证书私钥字符串
     * @return
     * @throws Exception
     */
    public static String getPrimaryKey(String privateKeyFileUri,String privateKeyFilePwd)throws Exception{
        KeyStore ks = getKs(privateKeyFileUri,privateKeyFilePwd);
        String keyAlias = getKeyAlias(privateKeyFileUri,privateKeyFilePwd);
        PrivateKey privateKey = (PrivateKey) ks.getKey(keyAlias, privateKeyFilePwd.toCharArray());
        String privateKeyStr = Base64.encodeBase64String(privateKey.getEncoded());
        return privateKeyStr;
    }

    /**
     * 获取证书公钥字符串
     * @return
     * @throws Exception
     */
    public static String getPublicKey(String privateKeyFileUri,String privateKeyFilePwd) throws Exception{
        KeyStore ks = getKs(privateKeyFileUri,privateKeyFilePwd);
        String keyAlias = getKeyAlias(privateKeyFileUri,privateKeyFilePwd);
        //公钥
        Certificate certificate = ks.getCertificate(keyAlias);

        certificate.getPublicKey().getAlgorithm();
        String publicKeyStr = Base64.encodeBase64String(certificate.getPublicKey().getEncoded());
        return publicKeyStr;
    }

    public static String getKeyAlias(String privateKeyFileUri,String privateKeyFilePwd)throws Exception{
        KeyStore ks = getKs(privateKeyFileUri,privateKeyFilePwd);
        @SuppressWarnings("rawtypes")
		Enumeration aliases = ks.aliases();
        String keyAlias = null;

        if (aliases.hasMoreElements()){
            keyAlias = (String)aliases.nextElement();
        }
        return keyAlias;
    }
    public static void main(String[] args) {
//        KeyStore ks;
//    	try {
//    		String keyAlias = getKeyAlias();
//            PrivateKey privateKey = (PrivateKey) ks.getKey(keyAlias, Config.getPrivateKeyPassword().toCharArray());
//            String privateKeyStr = Base64.encodeBase64String(privateKey.getEncoded());
//            System.out.println(privateKeyStr);
//    	}
//    	catch (Exception e) {
//			e.printStackTrace();
//		}
    	
	}


}
